package com.helasasa.maxfunds.lineofcredit.cashloan.splash.screen

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication
import com.helasasa.maxfunds.lineofcredit.cashloan.databinding.ActivityHelasasaSplashBinding
import com.helasasa.maxfunds.lineofcredit.cashloan.ghana.http.hitPoint
import com.helasasa.maxfunds.lineofcredit.cashloan.utils.analytics.util.AnalyticsUtil

class SplashHelasasaActivity : AppCompatActivity() {

    private lateinit var splashViewModel: SplashHelasasaViewModel
    private lateinit var binding: ActivityHelasasaSplashBinding

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 数据模型
        splashViewModel = ViewModelProvider(this)[SplashHelasasaViewModel::class.java]

        // 关联布局
        binding = ActivityHelasasaSplashBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 用到的控件
        val back = binding.navbarLeft
        val hotline = binding.navbarRight
        val loading = binding.loading

        // 时区接口监听
        splashViewModel.zoneResult.observe(this@SplashHelasasaActivity, Observer {
            val zoneResult = it ?: return@Observer

            loading.visibility = View.GONE
            if (zoneResult.error != null) {
                showMessage(zoneResult.error)
            }

            if (zoneResult.success != null) {
                splashViewModel.sysConfig()

                // 初始化 analysis
                AnalyticsUtil.init(ProjectApplication.instance)

                // 时区接口回来后，再打点
                hitPoint(type = this::class.java.simpleName, isPageStart = true)
            }
        })

        // 系统配置接口监听
        splashViewModel.sysConfigResult.observe(this@SplashHelasasaActivity, Observer {
            val sysConfigResult = it ?: return@Observer

            loading.visibility = View.GONE
            if (sysConfigResult.error != null) {
                showMessage(sysConfigResult.error)
            }

            if (sysConfigResult.success != null) {
                splashViewModel.checkUserState()
            }
        })

        // 流程接口监听
        splashViewModel.mainProcessResult.observe(this@SplashHelasasaActivity, Observer {
            val mainProcessResult = it ?: return@Observer

            loading.visibility = View.GONE
            if (mainProcessResult.error != null) {
                showMessage(mainProcessResult.error)
            }

            if (mainProcessResult.success != null) {
                splashViewModel.nextRouter(mainProcessResult.success)
            }
        })

        // 页面需要跳转监听
        splashViewModel.nextRouterResult.observe(this@SplashHelasasaActivity, Observer {
            val nextRouterResult = it ?: return@Observer

            loading.visibility = View.GONE
            startNextActivity(nextRouterResult.nextPage)
        })

        // 获取时区接口
        splashViewModel.zone()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
        hitPoint(type = this::class.java.simpleName, isPageEnd = true)
    }

    fun showMessage(message: String) {
        Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
    }

    fun startNextActivity(
        activity: Class<out AppCompatActivity>? = null,
        params: HashMap<String, String> = HashMap()
    ) {
        val nextActivity = activity ?: return
        try {
            val intent = Intent(this, nextActivity)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
            params.keys.forEach { gKey ->
                intent.putExtra(gKey, params[gKey])
            }
            startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
